﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Configuration;
using Microsoft.Win32;
using System.Reflection;

namespace DVDFilmoteka
{
    public static class Form1Servant
    {
        public static String CONFIG = Environment.CurrentDirectory + "\\config.xml";
        
        public static List<String> loadDataFromXML(String configFile, String XMLPath)
        {
            List<String> list = new List<string>();

            XmlDocument xml = new XmlDocument();
            xml.Load(configFile);

            XmlElement koren = xml.DocumentElement;
            XmlNodeList seznamUzlu = koren.SelectNodes(XMLPath);

            foreach (XmlNode uzel in seznamUzlu)
            {
                if (uzel.FirstChild != null && uzel.FirstChild.Value != null)
                {
                    list.Add(uzel.FirstChild.Value);
                }
                else
                {
                    list.Add("");
                }
            }

            return list;
        }
        
        public static List<String> getZanrNodeList()
        {
            return loadDataFromXML(CONFIG, "//root/mzanr/*");            
        }

        public static List<String> getCompresionNodeList()
        {
            return loadDataFromXML(CONFIG, "//root/mkomprese/*");
        }

        // Create a node sorter that implements the IComparer interface.
        public class NodeSorter : IComparer
        {
            // Compare the length of the strings, or the strings
            // themselves, if they are the same length.
            public int Compare(object x, object y)
            {
                TreeNode tx = x as TreeNode;
                TreeNode ty = y as TreeNode;

                if (tx.Tag == null || ty.Tag == null) return 0;

                // If they are the same length, call Compare.
                return string.Compare(tx.Text.ToUpper(), ty.Text.ToUpper());
            }
        }

        /// <summary>
        /// Otevre zadanou stranku v defaultnim webovem prohlizeci
        /// </summary>
        /// <param name="pageAddress"></param>
        public static void openPageInBrowser(String pageAddress)
        {            
            String path = Properties.Settings.Default.browserPath;            

            try {
                Process.Start(pageAddress);
            } catch (Exception) {
                try {
                    Process.Start(path, pageAddress);
                } catch (Exception) {
                    MessageBox.Show("Nelze spustit webový prohlížeč " + path + "\nUpravte prosím cestu v konfiguračním souboru aplikace.");
                }
            }
        }

        public static string AssemblyVersion
        {
            get
            {
                return "1.3BETA";
            }
        }

        public static string SaveFileVersion
        {
            get
            {
                return "1.2";
            }
        }

        public static string AssemblyName
        {
            get
            {
                return "DVD Filmotéka";
            }
        }
    }
}
